package psn.zjk.spring.rabbitmq.server;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import psn.zjk.spring.rabbitmq.client.MqClient;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author zhangjk
 * @CREATE_TIME 2020/11/25 17:09
 */
public class MqServer {
    public static final Logger LOGGER = LoggerFactory.getLogger(MqServer.class);

    private Channel channel;

    public MqServer() {
        try {
            init();
        } catch (Exception e) {
            LOGGER.error("", e);
        }
    }

    public void init() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
//        connectionFactory.setHost("192.168.99.100");
//        connectionFactory.setPort(32780);
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        //        创建连接
        Connection connection = connectionFactory.newConnection();
        //        在连接上建立渠道
        channel = connection.createChannel();
    }

    public Channel getChannel() {
        return channel;
    }

    public static void main(String[] args) throws IOException {
        MqServer mqServer = new MqServer();
        Channel channel = mqServer.getChannel();
        channel.basicPublish("x2","r2",null,"2222".getBytes());
        channel.basicPublish("x3","r4",null,"44444".getBytes());
    }

}